<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\gs_developer\model;

use app\common\model\BaseModel;

class DebugModel extends DevBaseModel
{
    protected $name = self::NAME_PREFIX . 'debug';

    protected $type = array(
        'request'  => 'json',
        'content'  => 'json',
        'value'    => 'json',
        'position' => 'json',
    );

    public function setIpAttr($value)
    {
        return ip2long($value);
    }

    public function getIpAttr($value)
    {
        return long2ip($value);
    }

    // 写入前执行
    public static function onBeforeInsert($row)
    {
        // 获取执行代码的文件和行数
        $trace = debug_backtrace();
        //位置
        $position = [];
        foreach ($trace as $key => $value) {
            if ($value['function'] == 'debug') {
                $position['file'] = str_replace(app()->getRootPath(), "/", $value['file']);
                $position['line'] = $value['line'];
                break;
            }
        }
        $row->position = $position;
        $row->request = request()->param();
        $row->ip = request()->ip();
        $row->addon = get_addon();
        $row->plaid = get_plaid();
        $row->content = request()->getContent();
        $row->client = request()->header('user-agent');
    }
}
